
#ifndef OGGSTREAM_H
#define OGGSTREAM_H   

#include "Sound.h"

#if defined(_WIN32)
#include <vorbis/vorbisfile.h>
#else
#include <vorbis/vorbisfile.h>
#endif

namespace al
{

enum STREAM_UPDATE_RESULT
{
    SUR_EOF = 0,
    SUR_OK = 1,
    SUR_LOOP,
    SUR_ERROR
};

class OGGstream
{
public:

    OGGstream();

    virtual ~OGGstream();

    /**
     * Opens tha specified file and initializes all structs neede in order to 
     * begin streaming from it.
     * @param fname
     * @return 
     */
    bool Open(const char* fname);

    /**
     * Release all resources of this.
     */
    void Release();

    void UnqueueBuffers(ALuint source);
    
    /**
     * Must be called after Open when Stream is ready to begin the streaming.
     * @param source - The source id of the Stream binded with this OGGStream.
     * @return 
     */
    bool InitializeStream(ALuint source);

    /**
     * Updtae the buffers of the stream if it's necessary.
     * @param source
     * @param loop
     * @return 
     */
    STREAM_UPDATE_RESULT Update(ALuint source,bool loop);

    /**
     * Rewinds the stream.
     * @param source
     */
    void Rewind(ALuint source);

protected:

    /**
     * Reloads the buffers if needed.
     * @param buffer
     * @return 
     */
    STREAM_UPDATE_RESULT Stream(ALuint buffer);

private:
    
    /**
     * The file being streamed.
     */
    FILE* oggFile; 

    /**
     * The ogg vorbis file handle.
     */
    OggVorbis_File oggStream;

    /**
     * Formating data.
     */
    vorbis_info* vorbisInfo;

    /**
     * Audio format.
     */
    ALuint format;

    /**
     * Front and backbuffer of the streamed file.
     */
    ALuint* buffers;
};

};

#endif
